            
class UserController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

   /* def list = {
        if(!params.max) params.max = 10
        [ userList: User.list( params ) ]
    }*/

    def show = {
        def user = User.get( params.id )

        if(!user) {
            flash.message = "User not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ user : user ] }
    }

   /* def delete = {
        def user = User.get( params.id )
        if(user) {
            user.delete()
            flash.message = "User ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "User not found with id ${params.id}"
            redirect(action:list)
        }
    }
*/
    def edit = {
        def user = User.get( params.id )

        if(!user) {
            flash.message = "User not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ user : user ]
        }
    }

    def update = {
        def user = User.get( params.id )
        if(user) {
            user.properties = params
            if(!user.hasErrors() && user.save()) {
                flash.message = "User ${params.id} updated"
                redirect(action:show,id:user.id)
            }
            else {
                render(view:'edit',model:[user:user])
            }
        }
        else {
            flash.message = "User not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def user = new User()
        user.properties = params
        return ['user':user]
    }

    def createNew = {
        flash.clear()
        redirect(action:create)
    }

    def save = {
        def user = new User(params)
        if(!user.hasErrors() && user.save()) {
            flash.message = "User ${user.email} created successfully. Login below."
            redirect(action: "login")
            //redirect(action:show,id:user.id)
        }
        else {
                render(view:"create", model:[user:user])
            //render(view:'create',model:[user:user])
        }
    }

    def login = {}
    def createAccount = {}



    def doLogin = {
            def user = new User();
        if (params.email == null || params.email == ""){
            user.errors.reject("email.blank","Please enter an email")
            render(view:"login", model:[user:user])
        }
        if (params.password == null || params.password == ""){
                    user.errors.reject("password.blank","Please enter a password")
                    render(view:"login", model:[user:user])
        }

        if(!user.hasErrors()){
            def u = User.findByEmailAndPassword(params.email, params.password)
            if (u) {
                session.user = u
                redirect(controller: "event", action: "home")
            } else {
                user.errors.reject("invalid.login","You have entered an invalid user name or password. Please try again or create a new account.")
                render(view:"login", model:[user:user])
            }
        }
    }

    def logout = {
        session.invalidate()
        flash.message = "You have logged out successfully"
        render(view:"login")
    }
}